package com.workcell.workdata.im.component.validator;

import com.workcell.workdata.im.annotation.validator.MessageFileFormat;
import com.workcell.workdata.im.constant.MessageFormat;
import com.workcell.workdata.im.model.message.ChatFile;
import com.workcell.workdata.im.model.request.MessageRequest;
import com.workcell.workdata.im.util.JSON;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

public class MessageFileValidator implements ConstraintValidator<MessageFileFormat, MessageRequest> {

    @Override
    public boolean isValid(MessageRequest momentRequest, ConstraintValidatorContext context) {

        if (momentRequest.getFormat() != MessageFormat.FILE){
            return true;
        }

        ChatFile file = JSON.parseNullable(momentRequest.getContent(), ChatFile.class);

        return file != null
                && file.getFile() != null
                && file.getName() != null;
    }
}
